// pages/personal_mse/phone.js
var that
var app=getApp();
var countdown = 60;
var settime = function (that) {
  if (countdown == 0) {
    that.setData({
      is_hidd: true
    })
    countdown = 60;
    return; 
  } else {
    that.setData({
      is_hidd: false,
      last_time: countdown
    })

    countdown--;
  }
  setTimeout(function () {
    settime(that)
  }, 1000)
}

Page({
  data: {
    picker1Value: 0,
    picker1Range: ['+86', '+80', '+84', '+87'],
    phone:"",
    code:"",
    is_hidd:true,
    last_time:'',
    op:'',  //是否是服务时不注册只留手机号
  },
  onLoad: function (options) {
    that=this;
    if (options.op = "op") {
      that.setData({ op: options.op})
      wx.setNavigationBarTitle({ title: '设置手机号' })
    }
  },
  onReady: function () {
  
  },
  onShow: function () {
  
  },
  area_code:function(e){//点击+86选择区号
    // console.log(e);
  },
  normalPickerBindchange: function (e) {
    this.setData({
      picker1Value: e.detail.value
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      phone: e.detail.value.trim()
    })
  },
  NameInput: function (e) {
    this.setData({
      code: e.detail.value.trim()
    })
  },
  get_code:function(){
    app.req("sendmess.html", { "openid": app.data.openid, phone: that.data.phone}, function (res) {
      if (res.status == "success") {
        // 将获取验证码按钮隐藏60s，60s后再次显示
        that.setData({ is_hidd: (!that.data.is_hidd)})
        settime(that);
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_code()
          }
        })
      }
    })
  },
  name_ok:function(){
    app.req("change_phone.html", { "openid": app.data.openid, phone: that.data.phone,icode:that.data.code}, function (res) {
      if (res.status == "success") {
        app.toast_up(res.mess)
        app.globalData.userInfo.phone = that.data.phone;
        app.data.person_msg_rr.mobile = that.data.phone;
        setTimeout(function () {
            wx.navigateBack({ delta: 1 })
        }, 1500) //延迟时间
      
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.name_ok()
          }
        })
      }
    })
  }
 
})